package cn.hhchat.wp.util;

import cn.hhchat.wp.entity.UserEntity;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created this one by HMH on 2017/7/31.
 */
public class SessionUtil {

    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static String getToken() {
        return getRequest().getHeader("authentication");
    }

    public static UserEntity getUser() {
        return (UserEntity) getSession().getAttribute("user");
    }

    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    public static void saveUserToSession(UserEntity userEntity) {
        getSession().setAttribute("user", userEntity);
    }


}
